package com.api.sw.duban.util;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.UUID;

public class MeetingWord {
    private XWPFDocument document;

    public XWPFDocument getDocument() {
        return document;
    }

    public void setDocument(XWPFDocument document) {
        this.document = document;
    }

    public MeetingWord(){
        this.document= new XWPFDocument();
        CTDocument1 document1 = document.getDocument();
        CTBody body = document1.getBody();
        if(!body.isSetSectPr()){
            body.addNewSectPr();
        }
        CTSectPr section = body.getSectPr();

        if(!section.isSetPgSz()){
            section.addNewPgSz();
        }
        //CTPageSz pageSize = section.getPgSz();
        //pageSize.setW(BigInteger.valueOf(W));
        //pageSize.setH(BigInteger.valueOf(H));
       // pageSize.setOrient(STPageOrientation.LANDSCAPE);
    }
    public static void main(String[] arg){
        System.out.println(UUID.randomUUID());
        WordExport WordExport=new WordExport(1,1);
        MeetingWord MeetingWord=new MeetingWord();
        XWPFDocument document1= MeetingWord.getDocument();
        XWPFParagraph titleParagraph = document1.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("参会人员名单");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(24);
        titleParagraphRun.addBreak();//换行
        setCTFonts(titleParagraph,"宋体");
        XWPFTable infoTable = document1.createTable(8,2);
        //infoTable.setTableAlignment(TableRowAlign.CENTER);//居中
        int[] colWidths={2300,6350};


        CTTbl ttbl = infoTable.getCTTbl();
        CTTblGrid tblGrid = ttbl.getTblGrid() != null ? ttbl.getTblGrid()
                : ttbl.addNewTblGrid();
        for (int j = 0, len = colWidths.length; j < len; j++) {
            CTTblGridCol gridCol = tblGrid.addNewGridCol();
            gridCol.setW(new BigInteger(String.valueOf(colWidths[j])));
        }
        //正常


        //CTTblWidth comTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        //comTableWidth.setType(STTblWidth.DXA);
        //comTableWidth.setW(BigInteger.valueOf(8650));
        List<XWPFTableRow> rows=infoTable.getRows();
        int i1=0;
        for(XWPFTableRow row :rows){
            List<XWPFTableCell> cells=row.getTableCells();
            XWPFTableCell cell=cells.get(0);
            XWPFTableCell cell1=cells.get(1);
            XWPFParagraph XWPFParagraph1=(XWPFParagraph)cell.getParagraphs().get(0);
            if(XWPFParagraph1==null){
                XWPFParagraph1= cell.addParagraph();
            }
            XWPFRun cellParagraphRun = XWPFParagraph1.createRun();
            cellParagraphRun.setText("王  中"+i1);
            cellParagraphRun.setColor("000000");
            cellParagraphRun.setFontSize(18);

            CTRPr rpr = cellParagraphRun.getCTR().isSetRPr() ? cellParagraphRun.getCTR().getRPr() : cellParagraphRun.getCTR().addNewRPr();
            CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
            fonts.setAscii("楷体_GB2312");
            fonts.setEastAsia("楷体_GB2312");
            fonts.setHAnsi("楷体_GB2312");

            //添加段落  第二列
            XWPFParagraph XWPFParagraphcell1=(XWPFParagraph)cell1.getParagraphs().get(0);

            XWPFRun cellParagraphRun2 = XWPFParagraphcell1.createRun();
            cellParagraphRun2.setText("(测试字体哈哈哈测试字体哈哈哈测试字体哈哈哈测试字体哈哈哈)");
            cellParagraphRun2.setColor("000000");
            cellParagraphRun2.setFontSize(18);

            CTRPr rpr2 = cellParagraphRun2.getCTR().isSetRPr() ? cellParagraphRun2.getCTR().getRPr() : cellParagraphRun2.getCTR().addNewRPr();
            CTFonts fonts2 = rpr2.isSetRFonts() ? rpr2.getRFonts() : rpr2.addNewRFonts();
            fonts2.setAscii("仿宋_GB2312");
            fonts2.setEastAsia("仿宋_GB2312");
            fonts2.setHAnsi("仿宋_GB2312");

            i1++;
        }
        try {
            //去掉单元格边框
            List<XWPFTableRow> rows1=infoTable.getRows();
            for(XWPFTableRow row :rows1) {
                List<XWPFTableCell> cells = row.getTableCells();
                XWPFTableCell cell = cells.get(0);
                CTTc CTTc=cell.getCTTc();

                CTTcBorders tblBorders =CTTc.addNewTcPr().addNewTcBorders();
                tblBorders.addNewLeft().setVal(STBorder.NIL);
                tblBorders.addNewRight().setVal(STBorder.NIL);
                tblBorders.addNewBottom().setVal(STBorder.NIL);
                tblBorders.addNewTop().setVal(STBorder.NIL);
                for(int i=0; i<row.getTableCells().size(); i++) {
                    row.getCell(i).getCTTc().addNewTcPr().setTcBorders(tblBorders);
                }
            }
            WordExport.saveDocument(document1,"D:"+ File.separator+"111.docx");
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    //设置字体
    public static void setCTFonts(XWPFParagraph XWPFParagraph1,String zt){
        try{
            XWPFRun ParagraphRun = XWPFParagraph1.createRun();
            CTRPr rpr2 = ParagraphRun.getCTR().isSetRPr() ? ParagraphRun.getCTR().getRPr() : ParagraphRun.getCTR().addNewRPr();
            CTFonts fonts2 = rpr2.isSetRFonts() ? rpr2.getRFonts() : rpr2.addNewRFonts();
            fonts2.setAscii(zt);
            fonts2.setEastAsia(zt);
            fonts2.setHAnsi(zt);
        }catch(Exception e){

        }
    }
}
